<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="物业名称" prop="pmid">
        <el-select v-model="queryParams.pmid" clearable>
          <el-option
            v-for="item in carsManagement"
            :key="item.pmid"
            :label="item.propertyName"
            :value="item.pmid"
            @keyup.enter.native="handleQuery">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="车场名称" prop="chid">
        <el-select v-model="queryParams.chid" clearable>
          <el-option
            v-for="item in carsHome"
            :key="item.chid"
            :label="item.chname"
            :value="item.chid"
            @keyup.enter.native="handleQuery">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="车辆类型" prop="ctid">
        <el-select v-model="queryParams.ctid" clearable>
          <el-option
            v-for="item in carsType"
            :key="item.ctid"
            :label="item.typeName"
            :value="item.ctid"
            @keyup.enter.native="handleQuery">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="车牌号">
        <el-input
          v-model="queryParams.licenseNumber"
          placeholder="请输入车牌号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="交易编号" prop="poid">
        <el-input
          v-model="queryParams.poid"
          placeholder="请输入交易编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="金额筛选">
        <el-select v-model="queryParams.orderPrice" placeholder="请选择" clearable>
          <el-option label="大于0元" :value="0" @keyup.enter.native="handleQuery"></el-option>
          <el-option label="大于3元" :value="3" @keyup.enter.native="handleQuery"></el-option>
          <el-option label="大于5元" :value="5" @keyup.enter.native="handleQuery"></el-option>
          <el-option label="大于8元" :value="8" @keyup.enter.native="handleQuery"></el-option>
          <el-option label="大于12元" :value="12" @keyup.enter.native="handleQuery"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="支付渠道" prop="ptid">
        <el-select v-model="queryParams.ptid" placeholder="请选择" clearable>
          <el-option
            v-for="item in payType"
            :key="item.ptid"
            :label="item.typeName"
            :value="item.ptid"
            @keyup.enter.native="handleQuery">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="支付状态" prop="poid">
        <el-select v-model="queryParams.orderStatus" placeholder="请选择" clearable>
          <el-option label="未支付" :value="0" @keyup.enter.native="handleQuery"></el-option>
          <el-option label="已支付" :value="1" @keyup.enter.native="handleQuery"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否优免">
        <el-select v-model="queryParams.youmian" placeholder="请选择" clearable>
          <el-option label="是" :value="1" @keyup.enter.native="handleQuery"></el-option>
          <el-option label="否" :value="0" @keyup.enter.native="handleQuery"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="daterangeCreateTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['pxy-report:park:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="parkList">
      <el-table-column label="序号" align="center" prop="parkid"/>
      <el-table-column label="物业名称" align="center" prop="propertyManagement.propertyName"/>
      <el-table-column label="车场名称" align="center" prop="carsHome.chname"/>
      <el-table-column label="车场id" align="center" prop="chid"/>
      <el-table-column label="车牌号" align="center" prop="carsMsg.licenseNumber"/>
      <el-table-column label="车辆类型" align="center" prop="carsType.typeName"/>
      <el-table-column label="订单金额" align="center" prop="payOrder.orderPrice"/>
      <el-table-column label="优免金额" align="center" prop=""/>
      <el-table-column label="优免说明" align="center" prop=""/>
      <el-table-column label="实付金额" align="center" prop="payOrder.orderPrice"/>
      <el-table-column label="支付类型" align="center" prop="payType.typeName"/>
      <el-table-column label="订单状态" align="center" prop="payOrder.orderStatus" :formatter="orderStatus"/>
      <el-table-column label="放行原因" align="center" prop="permit"/>
      <el-table-column label="入场时间" align="center" prop="createTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="出场时间" align="center" prop="updateTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="交易编号" align="center" prop="poid"/>
      <el-table-column
        fixed="right"
        label="操作"
        width="150"
        align="center"
        class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleList(scope.row)"
            v-hasPermi="['pxy-report:park:query']"
          >查看
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['pxy-report:park:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['pxy-report:park:remove']"
          >删除
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handlelists(scope.row)"
            v-hasPermi="['pxy-report:park:query']"
          >费用明细
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改停车报对话框 -->
    <el-dialog title="费用明细" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form1" :model="form1" label-width="80px">
        <table>
          <tr>
            {{ form1.createTime }}[{{ form1.carsMsg.licenseNumber }}]在{{ form1.carsHomeEq.eqname }}入场
          </tr>
          <tr style="line-height: 50px">
            {{ form1.updateTime }}[{{ form1.carsMsg.licenseNumber }}]在{{ form1.carsHomeEqChukou.ckname }}出场
          </tr>
          <tr>
            阶段时长: {{ Math.floor(((new Date(form1.updateTime)) - (new Date(form1.createTime))) / 60000) }}分钟
          </tr>
          <tr style="line-height: 50px">
            是否计费: 计费
          </tr>
          <tr style="line-height: 50px">
            车辆类型: {{ form1.carsType.typeName }}
          </tr>
          <tr>
            收费规则: {{ form1.carsHome.chname }}收费规则
          </tr>
        </table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <!--        <el-button type="primary" @click="submitForm">确 定</el-button>-->
        <el-button @click="cancel">返 回</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listPark, getPark, delPark, addPark, updatePark} from "@/api/pxy-report/park";
import {listHome} from "@/api/pxy-cars/home";
import {listManagement} from "@/api/pxy-cars/management";
import {getptApiList} from "@/api/pxy-report/paytype";
import {listType} from "@/api/pxy-cars/type";

export default {
  name: "Park",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 停车报表格数据
      parkList: [],
      carsHome: [],
      carsManagement: [],
      payType: [],
      carsType: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 放行原因时间范围
      daterangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        createTime: null,
        updateTime: null,
        pmid: null,
        chid: null,
        cmid: null,
        ctid: null,
        poid: null,
        ptid: null,
        permit: null,
        orderStatus: null,
        licenseNumber: null,
        orderPrice: null,
      },
      // 表单参数
      form: {},
      form1: {
        createTime: '',
        updateTime: '',
        carsMsg: {licenseNumber: ''},
        carsHomeEq: {eqname: ''},
        carsHomeEqChukou: {ckname: ''},
        carsType: {typeName: ''},
        carsHome: {chname: ''}
      },
    };
  },
  created() {
    this.getList();
    this.getHome();
    this.getManagement();
    this.getPayType();
    this.getType();
  },
  methods: {
    /** 查询停车报列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      listPark(this.queryParams).then(response => {
        this.parkList = response.rows;
        // console.log(response)
        this.total = response.total;
        this.loading = false;
      });
    },
    getHome() {
      this.loading = true;
      listHome(this.queryParams).then(response => {
        this.carsHome = response.rows;
        // console.log(response.rows)
        this.loading = false;
      });
    },
    getManagement() {
      this.loading = true;
      listManagement(this.queryParams).then(response => {
        this.carsManagement = response.rows;
        // console.log(response.rows)
        this.loading = false;
      });
    },
    getPayType() {
      this.loading = true;
      getptApiList(this.queryParams).then(response => {
        this.payType = response.rows;
        this.loading = false;
      });
    },
    getType() {
      this.loading = true;
      listType(this.queryParams).then(response => {
        this.carsType = response.rows;
        console.log(this.carsType)
        this.loading = false;
      });
    },
    handleList(row) {
      this.reset();
      const parkid = row.parkid || this.ids
      getPark(parkid).then(response => {
        this.form = response.data;
        console.log(this.form)
        this.$router.push({
          name: 'parkcheck',
          query: {form: this.form, isEditing: true}
        })
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const parkid = row.parkid || this.ids
      getPark(parkid).then(response => {
        this.form = response.data;
        this.$router.push({
          name: 'parkcheck',
          query: {form: this.form, isEditing: false}
        })
      });
    },
    orderStatus(row, col) {
      return row.payOrder.orderStatus === 0 ? '未支付' : '已支付'
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        parkid: null,
        code: null,
        status: null,
        deleted: null,
        createTime: null,
        updateTime: null,
        pmid: null,
        chid: null,
        cmid: null,
        ctid: null,
        poid: null,
        ptid: null,
        permit: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.queryParams = {}
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /**
     * 费用明细
     * @param row
     */
    handlelists(row) {
      this.reset();
      const parkid = row.parkid || this.ids
      getPark(parkid).then(response => {
        this.form1 = response.data;
        this.open = true;
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const parkids = row.parkid || this.ids;
      this.$modal.confirm('是否确认删除停车报编号为"' + parkids + '"的数据项？').then(function () {
        return delPark(parkids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('pxy-report/park/export', {
        ...this.queryParams
      }, `park_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
